<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/23
 * Time: 15:31
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\common\model\BuddhaCate;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MallCate;
use app\common\model\MallGoods;

class Cate extends Base
{
    public function getCateByList()
    {
        try {
            $lists = (new BuddhaCate())->field('id,title')
                ->where('status', Status::mysql_normal)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "内部错误");
        }
        return Show::json_show(Status::mini_normal, "Ok", $lists);
    }

    /**
     * @return array|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     */
    public function getCateByListInfo()
    {
        $select_id = $this->request->param('select_id', 0, 'intval');
        $child_id = $this->request->param('child_id', 0, 'intval');
        $page = $this->request->param('page', 1, 'intval');
        $limit = $this->request->param('limit', 10, 'intval');
        $keywords = $this->request->param('keywords','','trim');
        $obj_model = new MallCate();
        //获取第一次分类
        $menu = $obj_model->getBaseByList(['pid' => 0, 'status' => 1], 'id,title,image', 'sort desc,id desc');
        $lists = [];
        if (!empty($menu)) {
            foreach ($menu as $k => $v) {
                $lists[$v['id']] = $v;
                if ($select_id == 0) {
                    if ($k == 0) {
                        $lists[$v['id']]['selected'] = true;
                    } else {
                        $lists[$v['id']]['selected'] = false;
                    }
                } else {
                    if ($v['id'] == $select_id) {
                        $lists[$v['id']]['selected'] = true;
                    } else {
                        $lists[$v['id']]['selected'] = false;
                    }
                }


            }
            //获取选择的id
            $pid = 0;
            if ($select_id != 0) {
                $pid = $lists[$select_id]['id'];
            } else {
                $pid = array_values($lists)[0]['id'];
            }
            //查询第二级分类
            $child = $obj_model->getBaseByList(['pid' => $pid, 'status' => 1], 'id,title,image', 'sort desc,id desc');
            $child_l[] = [
                "id" => 0,
                "title" => "全部",
                "image" => ""
            ];
            $arr_child = array_merge($child_l, $child);
            if (!empty($child)) {
                if ($child_id == 0) {
                    //获取分类下面的全部商品
                    $arr_cate_ids = array_column($child, 'id');
                    $CateGoods = (new MallGoods())->getGoodsCateIds($arr_cate_ids, $this->shop_id, $page, $limit,$keywords,$this->m_openid);
                } else {
                    //获取分类商品
                    $CateGoods = (new MallGoods())->getGoodsCateIds([$child_id], $this->shop_id, $page, $limit,$keywords,$this->m_openid);
                }

            }
            return Show::json_show(Status::mini_normal, "ok", [
                "menu" => $lists ?? [],
                "child" => $arr_child ?? [],
                "cate_goods" => $CateGoods['data'] ?? [],
                "lat_price"=>sysconfig('shop','order_money') ?? '30'
            ], $CateGoods['total'] ?? 0, false, $CateGoods['last_page'] ?? 1);
        } else {
            //
            return Show::json_show(Status::mini_normal, "不存在");
        }

    }
}